package utils

import (
	"golang.org/x/text/encoding/simplifiedchinese"
	"golang.org/x/text/transform"
)

// GBKEncode 将UTF-8字符串编码为GBK字节
func GBKEncode(str string) ([]byte, error) {
	encoder := simplifiedchinese.GBK.NewEncoder()
	gbkBytes, _, err := transform.Bytes(encoder, []byte(str))
	if err != nil {
		return nil, err
	}
	return gbkBytes, nil
}

// GBKDecode 将GBK字节解码为UTF-8字符串
func GBKDecode(gbkBytes []byte) (string, error) {
	decoder := simplifiedchinese.GBK.NewDecoder()
	decodedBytes, _, err := transform.Bytes(decoder, gbkBytes)
	if err != nil {
		return "", err
	}
	return string(decodedBytes), nil
}
